<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>血糖记录表</title>
    <style>
        body {
          font-family: Arial, sans-serif;
          background-color: #f4f4f4;
          margin: 20px;
        }

       .top-row {
          display: flex;
          align-items: center;
          margin-bottom: 10px;
        }

       .top-row span,
       .top-row select {
            font-size: 16px;
        }

        select {
          padding: 8px;
          border: 1px solid #ccc;
          border-radius: 4px;
        }

        #lowBloodText {
          color: #007bff;
        }

        #highBloodText {
          color: #dc3545;
        }

        table {
          border-collapse: collapse;
          max-width: 40%;
          margin: 0 auto;
          background-color: white;
          box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
        }

        table,
        th,
        td {
          border: 1px solid #e0e0e0;
        }

        th,
        td {
          text-align: center;
          padding: 8px;
        }

        th {
          background-color: #e6f7ff;
          font-weight: normal;
        }
    </style>
</head>

<body>
<div class="top-row">
    <span>时间：</span>
    <select id="yearSelect">
    </select>
    <select id="monthSelect">
    </select>
    <span id="lowBloodText">血糖值偏低</span>
    <span id="highBloodText">血糖值偏高</span>
</div>
<table id="bloodTable">
    <thead>
    <tr>
        <th>日期</th>
        <th>凌晨</th>
        <th colspan="2">早餐</th>
        <th colspan="2">午餐</th>
        <th colspan="2">晚餐</th>
        <th>睡前</th>
    </tr>
    <tr>
        <th></th>
        <th></th>
        <th>空腹</th>
        <th>后</th>
        <th>前</th>
        <th>后</th>
        <th>前</th>
        <th>后</th>
        <th></th>
    </tr>
    </thead>
    <tbody id="tableBody">
    </tbody>
</table>

<script>
    const currentDate = new Date();
    const currentYear = currentDate.getFullYear();
    const currentMonth = currentDate.getMonth();

    let prevYear = currentYear;
    let prevMonth = currentMonth === 0? 11 : currentMonth - 1;
    if (prevMonth === 11) {
      prevYear--;
    }

    const yearSelect = document.getElementById('yearSelect');
    const monthSelect = document.getElementById('monthSelect');

    // 只展示三年的数据
    const startYear = currentYear - 2;
    for (let i = startYear; i <= currentYear; i++) {
      const option = document.createElement('option');
      option.value = i;
      option.textContent = i + '年';
      yearSelect.appendChild(option);
      if (i === prevYear) {
        option.selected = true;
      }
    }
    for (let i = 1; i <= 12; i++) {
      const option = document.createElement('option');
      option.value = i;
      option.textContent = i + '月';
      monthSelect.appendChild(option);
      if (i === prevMonth + 1) {
        option.selected = true;
      }
    }

    const tableBody = document.getElementById('tableBody');

    function getDaysInMonth(year, month) {
      return new Date(year, month, 0).getDate();
    }

    function generateRandomBloodData() {
      return (Math.random() * 4 + 4).toFixed(1);
    }

    function checkBloodStatus(value) {
      const numValue = parseFloat(value);
      if (numValue < 5) {
        return 'low';
      } else if (numValue > 7.5) {
        return 'high';
      }
      return '';
    }

    function renderTable(year, month) {
      tableBody.innerHTML = '';
      const daysInMonth = getDaysInMonth(year, month);
      const now = new Date();
      for (let day = 1; day <= daysInMonth; day++) {
        const row = document.createElement('tr');
        const dayCell = document.createElement('td');
        dayCell.textContent = day;
        row.appendChild(dayCell);
        const targetDate = new Date(year, month, day);
        const hasPassed = targetDate <= now;
        for (let i = 0; i < 8; i++) {
          const cell = document.createElement('td');
          if (hasPassed) {
            const data = generateRandomBloodData();
            cell.textContent = data;
            const status = checkBloodStatus(data);
            if (status === 'low') {
              cell.style.color = '#007bff';
            } else if (status === 'high') {
              cell.style.color = '#dc3545';
            }
          } else {
            cell.textContent = '';
          }
          row.appendChild(cell);
        }
        tableBody.appendChild(row);
      }
    }

    renderTable(prevYear, prevMonth);

    yearSelect.addEventListener('change', () => {
      renderTable(parseInt(yearSelect.value), parseInt(monthSelect.value) - 1);
    });
    monthSelect.addEventListener('change', () => {
      renderTable(parseInt(yearSelect.value), parseInt(monthSelect.value) - 1);
    });
</script>
</body>

</html>